/* eslint-disable @typescript-eslint/indent */
import * as fs from 'fs'
import { AKDict } from './types'

export const ValidLangs = [ 'zh_CN', 'zh_TW', 'en_US', 'ja_JP', 'ko_KR' ]
export const ValidExcels = fs.readdirSync('api/ArknightsGameData/zh_CN/gamedata/excel')
                             .map(x => x.replace('.json', ''))

export const Cache: AKDict<any> = {}

export const langKey = (key: string, lang = 'zh_CN'): string => {
    return `${key}@${lang}`
}

export const load = (pathname: string, key: string): any => {
    if (!Cache[key] && fs.existsSync(pathname)) {
        console.log(`loading ${pathname} -> ${key}`)
        Cache[key] = JSON.parse(fs.readFileSync(pathname, 'utf-8'))
    }
    return Cache[key]
}

export const getExcel = (key: string, lang = 'zh_CN'): any => {
    return load(`api/ArknightsGameData/${lang}/gamedata/excel/${key}.json`, langKey(key, lang))
}

export const getLevels = (path: string, lang = 'zh_CN'): any => {
    return load(`api/ArknightsGameData/${lang}/gamedata/levels/${path}.json`, langKey(path, lang))
}

export const getCustom = (filename: string): any => {
    return load(`api/customdata/${filename}.json`, filename)
}
